/**
 * @description shop router
 * @author boen
 */

const router = require('koa-router')()
const { ErrorModel, SuccessModel} = require('../res-model/index')
const {
    getHotList,
    getShopInfo,
    getProductsByShopId
} = require('../controller/shop')

// 前缀
router.prefix("/api/shop")

// 附近热门店铺列表
router.get('/hot-list', async (ctx, next) => {
    const hotList = await getHotList()
    if(hotList) {
        ctx.body = new SuccessModel(hotList)
    }else {
        ctx.body = new ErrorModel(10007,"查询附近热门商铺失败")
    }
})

// 商店详情
router.get('/:id', async(ctx, next) => {
    const id = ctx.params.id
    const shopInfo = await getShopInfo(id)
    if(shopInfo) {
        ctx.body = new SuccessModel(shopInfo)
    }else {
        ctx.body = new ErrorModel(10008,"查询商店详情信息失败")
    } 
})

// 查询某个商店的商品列表
router.get('/:id/products', async (ctx, next) => {
    const shopId = ctx.params.id
    const query = ctx.query
    const tab = query.tab || 'all' // 默认为all
    const productList = await getProductsByShopId(shopId, tab)
    if(productList) {
        ctx.body = new SuccessModel(productList)
    }else {
        ctx.body = new ErrorModel(10009,"查询商品列表失败")
    }
})

module.exports = router